package com.boang.time;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class SecKillCountdown {
    
    public static void main(String[] args) {
        // 设置秒杀开始的时间（这里假设是2023年10月1日 12:00:00）
        LocalDateTime flashSaleStartTime = LocalDateTime.of(2024, 11, 22, 14, 30, 0);
        
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        
        // 计算剩余时间
        Duration remainingTime = Duration.between(now, flashSaleStartTime);
        
        // 打印倒计时
        printCountdown(remainingTime);
        
        // 模拟每秒更新一次倒计时（在实际应用中，应该使用定时任务）
        while (remainingTime.getSeconds() > 0) {
            try {
                Thread.sleep(1000); // 休眠1秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            now = LocalDateTime.now();
            remainingTime = Duration.between(now, flashSaleStartTime);

            System.out.print("\r"); // 覆盖上一行
            printCountdown(remainingTime);
        }

        System.out.println("秒杀开始！");
    }
    
    private static void printCountdown(Duration duration) {
        long days = duration.toDays();
        long hours = duration.toHours() % 24;
        long minutes = duration.toMinutes() % 60;
        long seconds = duration.getSeconds() % 60;
        
        System.out.printf("距离秒杀开始还有: %02d天 %02d小时 %02d分钟 %02d秒%n", days, hours, minutes, seconds);
    }
}